<template>
  <div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      height="300"
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.id_ }}
        </template>
      </el-table-column>
      <template v-for="(header, index) in subTableConfig.tableHeaders">
        <el-table-column
          :key="index"
          :label="header.name"
        >
          <template slot-scope="scope">
            {{ scope.row[header.field] }}
          </template>
        </el-table-column>
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>

<script>
import { getModelData } from '@/api/normal'
export default {
  name: 'ConfigAbleTable',
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  props: {
    tableHeaders: {
      type: Array,
      default: undefined
    },
    title: {
      type: String,
      default: undefined
    },
    name: {
      type: String,
      default: undefined
    },
    eventName: {
      type: String,
      default: undefined
    }
  },
  data() {
    return {
      list: null,
      subTableConfig: {
        tableHeaders: this.tableHeaders,
        title: this.title,
        name: this.name,
        eventName: this.eventName
      },
      listLoading: true
    }
  },
  created() {
    this.httpFetchData()
    console.log('configAbleTable')
    this.$eventBus.subEventHandler(this.eventName, this.wsFetchData)
  },
  methods: {
    httpFetchData() {
      this.listLoading = true
      console.log('----------------------', this.subTableConfig)
      getModelData({ event_name: this.subTableConfig.eventName }).then(response => {
        if (response.code === 20000) {
          this.list = response.data
        }
      })
      this.listLoading = false
    },
    wsFetchData(data) {
      this.$set(this.list, data.id_, data)
    }
  }
}
</script>
